//
//  ForgiveViewController.m
//  LiuHeBao
//
//  Created by zr on 2018/8/22.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "ForgiveViewController.h"

@interface ForgiveViewController ()

@property (nonatomic, weak)UIButton * backButton;
@property (nonatomic, weak)UILabel * titleLabel;
@property (nonatomic, weak)UIImageView * backImageView;
@property (nonatomic, strong)UITextField * accountTextFiled;
@property (nonatomic, strong)UITextField * verTextFiled;
@property (nonatomic, strong)UITextField * passwordTextFiled;
@property (nonatomic, strong)UITextField * repasswordTextFiled;
@property (nonatomic, weak)UIButton * verButton;
@property (nonatomic, weak)UIButton * loginButton;
//计时器
@property (nonatomic, strong)NSTimer * countDownTimer;

@end

//倒计时总的秒数
static NSInteger  secondsCountDown = 60;

@implementation ForgiveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupUI];
}


- (void)setupUI{
    
    UIImageView * backImageView = [UIImageView new];
    
    backImageView.image = ImageNamed(@"bg");
    backImageView.userInteractionEnabled = YES;
    
    self.backImageView = backImageView;
    
    [self.view addSubview:self.backImageView];
    
    
    UIButton * backButton = [UIButton new];
    
    [backButton setImage:ImageNamed(@"1返回") forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(clickBackButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.backButton = backButton;
    
    [self.backImageView addSubview:self.backButton];
    
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.textColor = WHITE_COLOR;
    titleLabel.font = [UIFont systemFontOfSize:18 * SCREENH6];
    titleLabel.text = @"忘记密码";
    
    self.titleLabel = titleLabel;
    
    [self.backImageView addSubview:self.titleLabel];
    
    
    self.accountTextFiled = [self setupTextFiledWith:@"请输入账号"];
    [self.backImageView addSubview:self.accountTextFiled];
    
    self.verTextFiled = [self setupTextFiledWith:@"请输入验证码"];
    [self.backImageView addSubview:self.verTextFiled];
    
    self.passwordTextFiled = [self setupTextFiledWith:@"请输入您要修改的密码"];
    self.passwordTextFiled.secureTextEntry = YES;
    [self.backImageView addSubview:self.passwordTextFiled];
    
    self.repasswordTextFiled = [self setupTextFiledWith:@"请再次确认您的密码"];
    self.repasswordTextFiled.secureTextEntry = YES;
    [self.backImageView addSubview:self.repasswordTextFiled];
    
    
    UIButton * verButton = [UIButton new];
    
    [verButton setBackgroundColor:[UIColor clearColor]];
    [verButton setTitle:@"发送验证码" forState:UIControlStateNormal];
    [verButton setTitleColor:ColorWithRGB(0xDA4136) forState:UIControlStateNormal];
    [verButton addTarget:self action:@selector(clickVerButton) forControlEvents:UIControlEventTouchUpInside];
    verButton.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    verButton.layer.borderColor = [ColorWithRGB(0xDA4136) CGColor];
    verButton.layer.borderWidth = 1.0f;
    
    self.verButton = verButton;
    
    [self.backImageView addSubview:self.verButton];
    
    
    UIButton * loginBurron = [UIButton new];
    
    [loginBurron setBackgroundColor:ColorWithRGB(0xDA4136)];
    [loginBurron setTitle:@"确认提交" forState:UIControlStateNormal];
    [loginBurron setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
    [loginBurron addTarget:self action:@selector(clickLoginButton) forControlEvents:UIControlEventTouchUpInside];
    loginBurron.titleLabel.font = [UIFont systemFontOfSize:17 * SCREENH6];
    
    self.loginButton = loginBurron;
    
    [self.backImageView addSubview:self.loginButton];
    
}


- (void)viewDidLayoutSubviews{
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.bottom.mas_equalTo(self.view);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.backImageView);
        make.top.mas_equalTo(self.backImageView).offset(30 * SCREENH6);
    }];
    
    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backImageView).offset(15 * SCREENH6);
        make.centerY.mas_equalTo(self.titleLabel);
        make.width.mas_equalTo(12 * SCREENH6);
        make.height.mas_equalTo(20 * SCREENH6);
    }];
    
    [self.accountTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backImageView).offset(62.5 * SCREENH6);
        make.trailing.mas_equalTo(self.backImageView).offset(-62.5 * SCREENH6);
        make.height.mas_equalTo(40 * SCREENH6);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(180 * SCREENH6);
    }];
    
    [self.verTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.height.mas_equalTo(self.accountTextFiled);
        make.width.mas_equalTo(125 * SCREENH6);
        make.top.mas_equalTo(self.accountTextFiled.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.verButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.height.mas_equalTo(self.accountTextFiled);
        make.centerY.mas_equalTo(self.verTextFiled);
        make.leading.mas_equalTo(self.verTextFiled.mas_trailing).offset(20 * SCREENH6);
    }];
    
    [self.passwordTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.accountTextFiled);
        make.top.mas_equalTo(self.verTextFiled.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.repasswordTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.passwordTextFiled);
        make.top.mas_equalTo(self.passwordTextFiled.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.repasswordTextFiled);
        make.top.mas_equalTo(self.repasswordTextFiled.mas_bottom).offset(25 * SCREENH6);
    }];
    
}


- (UITextField *)setupTextFiledWith:(NSString *)str{
    
    UITextField * textFiled = [UITextField new];
    
    textFiled.borderStyle = UITextBorderStyleLine;
    textFiled.backgroundColor = [UIColor clearColor];
    textFiled.layer.borderColor = [WHITE_COLOR CGColor];
    textFiled.layer.borderWidth = 1.0f;
    //设置光标颜色
    textFiled.tintColor = [UIColor whiteColor];
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = WHITE_COLOR;//[UIColor lightGrayColor];
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:str attributes:attrs];
    textFiled.textColor = [UIColor whiteColor];
    textFiled.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22 * SCREENH6, 0)];
    //设置显示模式为永远显示(默认不显示)
    textFiled.leftViewMode = UITextFieldViewModeAlways;
    textFiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    textFiled.keyboardType = UIKeyboardTypeNumberPad;
    textFiled.keyboardAppearance = UIKeyboardAppearanceDark;
    
    return textFiled;
}


- (void)setupDownTimer{
    
    _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownAction) userInfo:nil repeats:YES];
}

//定时器的方法
- (void)countDownAction{
    
    secondsCountDown --;
    
    [self.verButton setTitle:[NSString stringWithFormat:@"  %lds后获取  ",(long)secondsCountDown] forState:UIControlStateDisabled];
    
    [self.verButton setBackgroundColor:[UIColor lightGrayColor]];
    
    //当倒计时到0时做需要的操作，button变成可选状态
    if(secondsCountDown == 0){
        
        [_countDownTimer invalidate];
        
        [self.verButton setBackgroundColor:[UIColor whiteColor]];
        [self.verButton setTitle:@"  获取验证码  " forState:UIControlStateNormal];
        self.verButton.enabled = YES;
    }
}


- (void)clickLoginButton{
    
    [self.accountTextFiled resignFirstResponder];
    [self.verTextFiled resignFirstResponder];
    [self.passwordTextFiled resignFirstResponder];
    [self.repasswordTextFiled resignFirstResponder];
    
    if ([self.accountTextFiled.text isEqualToString:@""] || [self.verTextFiled.text isEqualToString:@""] || [self.passwordTextFiled.text isEqualToString:@""] || [self.repasswordTextFiled.text isEqualToString:@""]) {
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"请完善注册信息！"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  //响应事件
                                                                  
                                                              }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
        
    }else{
        
        SHOWHUD;
        
        NSDictionary * parametersDic = @{@"mobile":self.accountTextFiled.text,@"password":self.repasswordTextFiled.text,@"code":self.verTextFiled.text};
        
        [[NetworkTool sharedTools] POST:@"/index/Member/forgetpwd" parameters:parametersDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
            
            HIDDENHUD;
            
//            NSLog(@"res===%@",responseObject);
            
            NSString * codeString = [NSString stringWithFormat:@"%@",responseObject[@"status"]];
            
            if ([codeString isEqualToString:@"200"]) {
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:@"恭喜你修改成功！赶快去登录吧！"
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          
                                                                          [self.navigationController popViewControllerAnimated:YES];
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
                
            }else{
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:responseObject[@"msg"]
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
                
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            HIDDENHUD;
            
            NSLog(@"error===%@",error);
        }];
        
    }
}


- (void)clickVerButton{
    
    self.verButton.enabled = NO;
    secondsCountDown = 60;
    
    [self setupDownTimer];
}


-(void)clickBackButton{
    
    [self.navigationController popViewControllerAnimated:YES];
}
@end
